import java.util.Arrays;

public class Solution {

    public static void main(String[] args) {
        int[] heights = {4, 1, 6, 5};
        long res = Solution.minimumRemoval(heights);
        System.out.println(res);
    }

    public static long minimumRemoval(int[] beans) {
        Arrays.sort(beans);
        long[] lbeans = Arrays.stream(beans).mapToLong(Long::valueOf).toArray();
        long sum = Arrays.stream(lbeans).sum();

        long res = Long.MAX_VALUE;
        long tmp;
        for (int i = 0; i < lbeans.length; i++) {
            tmp = sum - lbeans[i] * (lbeans.length - i);
            res = Math.min(tmp, res);
        }
        return res;
    }

    public static long mininumRemoval(int[] beans) {
        int[] nums = new int[100001];
        long total = 0;
        int n = beans.length;
        for (int bean : beans) {
            nums[bean]++;
            total += bean;
        }

        long res = total;
        for (long i = 1; i < 100001; i++) {
            //i*n是让剩下的袋子中所有豆子数目都为i，而用总数total减去他，就能获得要拿出的数量
            res = Math.min(res, total - i * n);
            // **n作为袋子数目，要时刻记录大于等于i的袋子数**
            n -= nums[(int) i];//移除等于当前魔法豆数量的袋子数
        }
        return res;
    }
}
